<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <div id="app">


  </div>
  <!-- 引入React依赖 -->
  <script src="../React/react.development.js"></script>
  <script src="../React/react-dom.development.js"></script>
  <script src="../React/babel.min.js"></script>

  <!-- 编写React代码 -->
  <script type="text/babel">
    class App extends React.Component {
      constructor() {
        super()
        //定义变量
        this.state = {
          count: 0,
        }
      }
      render() {
        //如果函数中需要使用this则需要通过 .bind(this) 来绑定，因为函数是由React调用
        return (
          <div>
            <h2>当前计数：{this.state.count}</h2>
            <button onClick={this.increment.bind(this)}>+1</button>
            <button onClick={this.decrement.bind(this)}>-1</button>
          </div>
        )
      }

      increment() {
        // this.state.count = this.state.count + 1
        // console.log(this.state.count);

        //实时更新数据需要使用 setState() 方法来更新
        this.setState({
          count: this.state.count + 1
        })
      }
      decrement() {
        // this.state.count = this.state.count - 1
        // console.log(this.state.count);

         //实时更新数据需要使用 setState() 方法来更新
        this.setState({
          count: this.state.count - 1
        })
      }
    }
  </script>

  <script type="text/babel">
    ReactDOM.render(<App />, document.getElementById("app"))
  </script>

</body>

</html>